<style lang="less">
	/*@import './international.less';*/
</style>

<template>
	<div>
		<common-page v-bind="menuPage" @tableOnRowDblclick="tableOnRowDblclick" ref="menu">
			<template slot="modal">
				<Form :model="menuPage.detailData" :label-width="80">
					<FormItem label="消息类型">
						<autoSelect :model.sync="menuPage.detailData.type" url="dictionaryinfo/list" textField="text"
									valueField="sn"
									:params="{psn:'messageType', state:'1'}"></autoSelect>
					</FormItem>
					<FormItem label="接收者类型">
						<autoSelect :model.sync="menuPage.detailData.receiverType" url="dictionaryinfo/list"
									textField="text" valueField="sn"
									:params="{psn:'messageReceiverType', state:'1'}"></autoSelect>
					</FormItem>
					<FormItem label="接受者">
						<template v-if="menuPage.detailData.receiverType === '1'">
							<autoSelect :model.sync="menuPage.detailData.receiver" url="role/list" textField="text" valueField="sn"
										:params="{psn:'messageReceiverType', state:'1'}"></autoSelect>
						</template>
						<template v-else>
							<Input v-model="menuPage.detailData.receiver" placeholder="填写接受者的用户名,比如admin"></Input>
						</template>
					</FormItem>
					<!--短信-->
					<template v-if="+menuPage.detailData.type == '3'">
						<FormItem label="短信模板选择">
							<autoSelect :model.sync="menuPage.detailData.templateId" url="shortmessagetemplate/list"
										textField="text" valueField="id" :clearable="false"
										@autoSelect_onChange="autoSelectOnChange"></autoSelect>
						</FormItem>
						<div style="text-align: center">模板参数 ↓ </div>
						<template v-for="param in templateParam">
							<FormItem :label="param.name">
								<Input v-model="menuPage.detailData.realTemplateParamsMap[param.name]" :value="param.value"
									   :placeholder="param.value"></Input>
							</FormItem>
						</template>
					</template>
					<template v-else>
						<FormItem label="主题">
							<Input v-model="menuPage.detailData.topic" placeholder="Email和站内信的主题 短信不需要"></Input>
						</FormItem>
						<FormItem label="消息">
							<Input v-model="menuPage.detailData.message" placeholder="消息内容 内置变量 ${to}=接受人用户名"
								   type="textarea" :rows="5"></Input>
						</FormItem>
					</template>
					<hr>
					<br>
					<FormItem label="添加时间">
						<Input v-model="menuPage.detailData.addTime" :readonly="true" placeholder="系统生成"></Input>
					</FormItem>
					<FormItem label="编号">
						<Input v-model="menuPage.detailData.sn" :readonly="true" placeholder="系统生成,用于查找信息发送结果"></Input>
					</FormItem>
					<FormItem label="进度">
						<autoSelect :model.sync="menuPage.detailData.progress" url="dictionaryinfo/list"
									textField="text" valueField="sn"
									:params="{psn:'messageSendProgress', state:'1'}" :disabled="true"></autoSelect>
					</FormItem>
				</Form>
			</template>
		</common-page>
		<br>
		<h2 :style="{textAlign:'center'}">双击上方记录行,下方显示详细信息</h2>
		<br>
		<common-page v-bind="menuPage2" ref="info"></common-page>
	</div>
</template>

<script>

    import commonPage from '@/views/common/common-page.vue';
    import util from '@/libs/util';
    import autoSelect from '@/views/common/autoSelect.vue';

    // 默认界面查询条件
    let pageParams = Object.assign({}, util.queryParams, { // 额外的参数,比如条件的默认值
        sortName: "addTime",
        sortOrder: "desc"
    });
    let pageInfoParams = Object.assign({}, util.queryParams, { // 额外的参数,比如条件的默认值
        sortName: "date",
        sortOrder: "desc",
        sn: "_NONE"
    });
    export default {
        name: 'messagesender-page',
        components: {
            commonPage,
            autoSelect
        },
        props: {
            columns: Array
        },
        data () {
            return {
                templates: [], // 短信模板下拉框
                templateParam: [], // 短信模板参数
                menuPage: {
                    toolbar: {
                        menuSn: "messagesender",
                        buttons: []
                    },
                    list: {
                        table: {
                            columns: [
                                {type: 'selection', width: 50, align: 'center'},
                                {type: 'index', width: 60, align: 'center'},
                                {title: 'ID', key: 'id'},
                                {title: '编号', key: 'sn'},
                                {title: '消息类型', key: 'typeName'},
                                {title: '进度', key: 'progressName'},
                                {title: '发送人', key: 'senderName'},
                                {title: '接受者', key: 'receiver'},
                                {title: '接受者类型', key: 'receiverTypeName'},
//                                {title: '主题', key: 'topic'},
                                {title: '添加时间', key: 'addTime', width: 180}
                            ],
                            data: []
                        },
                        url: "messagesender/list",
                        queryParams: pageParams
                    },
                    searchbar: {
                        queryParams: pageParams,
                        inputs: [
                            {
                                text: "编号",
                                sn: "sn",
                                style: {
                                    width: "200px"
                                }
                            },
                            {
                                text: "消息类型",
                                sn: "type",
                                type: "autoSelect",
                                clearable: true,
                                url: "dictionaryinfo/list",
                                params: {
                                    psn: "messageType",
                                    state: "1"
                                },
                                valueField: "sn",
                                textField: "text",
                                style: {
                                    width: "200px"
                                }
                            },
                            {
                                text: "进度",
                                sn: "progress",
                                type: "autoSelect",
                                clearable: true,
                                url: "dictionaryinfo/list",
                                params: {
                                    psn: "messageSendProgress",
                                    state: "1"
                                },
                                valueField: "sn",
                                textField: "text",
                                style: {
                                    width: "200px"
                                }
                            },
                            {
                                text: "接收人",
                                sn: "receiver",
                                style: {
                                    width: "200px"
                                }
                            },
                            {
                                text: "主题",
                                sn: "topic",
                                style: {
                                    width: "200px"
                                }
                            },
                            {
                                text: "消息",
                                sn: "message",
                                style: {
                                    width: "200px"
                                }
                            },
                            {
                                text: "日期起",
                                sn: "addTimeStart",
                                type: "datetime",
                                format: "yyyy-MM-dd HH:mm:ss",
                                style: {
                                    width: "100px"
                                }
                            }, {
                                text: "日期止",
                                sn: "addTimeEnd",
                                type: "datetime",
                                format: "yyyy-MM-dd HH:mm:ss",
                                style: {
                                    width: "100px"
                                }
                            }
                        ]
                    },
                    pageParams: pageParams,
                    detailData: {
                        id: "",
                        sn: "",
                        type: "",
                        progress: "",
                        sender: "",
                        receiver: "admin",
                        receiverType: "",
                        topic: "",
                        message: "",
                        addTime: "",
                        templateId: null,
                        realTemplateParamsMap: {}
                    }
                },
                menuPage2: {
                    toolbar: {
                        menuSn: "messagesenderinfo",
                        buttons: []
                    },
                    list: {
                        table: {
                            columns: [
                                {type: 'selection', width: 50, align: 'center'},
                                {type: 'index', width: 60, align: 'center'},
                                {title: '编码', key: 'sn'},
                                {title: '接受者', key: 'username'},
                                {title: '发送时间', key: 'date'},
                                {
                                    title: '结果', key: 'result', render: (h, obj) => {
                                    if (obj.row.result === "1") {
                                        return "成功";
                                    } else if (obj.row.result === "2") {

                                        return "失败!" + obj.row.failMessage;
                                    }
                                    return obj.row.state;
                                }
                                }

                            ],
                            data: []
                        },
                        url: "messagesenderinfo/list",
                        queryParams: pageInfoParams
                    },
                    searchbar: {
                        queryParams: pageInfoParams,
                        hideButtons: true
                    },
                    pageParams: pageInfoParams,
                    detailData: {
                        username: "",
                        sn: "",
                        date: "",
                        result: "1",
                        failMessage: ""

                    },
                    form: null
                }
            };
        },
        methods: {
            tableOnRowDblclick(obj, index) {
                let me = this;
                pageInfoParams.sn = obj.sn;
                me.$refs.menu.getInitDetailData().sn = obj.sn;
                me.$refs.info.$refs.list.load();
            },
            autoSelectOnChange(value, index, arr) {
                let me = this;
                let i = 0;
                let param = JSON.parse(value.templateParam);
                me.templates = [];
                me.templateParam = [];
                me.menuPage.detailData.realTemplateParamsMap = {};
                for (let k in param) {
                    me.templateParam[i++] = {
                        name: k,
                        value: param[k]
                    };
                    me.menuPage.detailData.realTemplateParamsMap[k] = param[k];
                }
            }
        },
        created () {
        },
        mounted() {
        },
        watch: {
            menuPage(newVal){
                let me = this;
                if (+newVal.detailData.type !== 3) { // 短信发送
                    me.templates = [];
                    me.templateParam = [];
                }
            }
        }
    };
</script>
